package cn.iocoder.yudao.module.promotion.groupon.service.admin;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.order.service.AdminOrderBaseService;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.api.promotion.groupon.GrouponPackageApi;
import cn.iocoder.yudao.module.api.promotion.groupon.dto.GrouponPackageRespDTO;
import cn.iocoder.yudao.module.promotion.groupon.dao.GrouponOrderMapper;
import cn.iocoder.yudao.module.promotion.groupon.pojo.dto.GrouponOrderQueryDTO;
import cn.iocoder.yudao.module.promotion.groupon.pojo.po.GrouponOrder;
import cn.iocoder.yudao.module.promotion.groupon.pojo.vo.GrouponOrderVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * <pre>
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * 服务
 * OOoO0OOoO0OOOooo0oOOOO0OOOOO0oooOO0ooOOO0Ooooo0OOOOo0ooooO0OOooo0Ooooo0OOOOO
 * </pre>
 *
 * @author 山野羡民（1032694760@qq.com）
 * @since 2025/05/14
 */
@Service
public class GrouponOrderService extends AdminOrderBaseService<GrouponOrder, GrouponOrderVO, GrouponOrderQueryDTO> {
    @Resource
    private GrouponOrderMapper grouponOrderMapper;

    @Resource
    private MemberUserApi memberUserApi;
    @Resource
    private GrouponPackageApi grouponPackageApi;

    @Override
    public GrouponOrderMapper getInjectedMapper() {
        return grouponOrderMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<GrouponOrder> buildQueryWrapper(GrouponOrderQueryDTO param) {
        CoreQueryWrapper<GrouponOrder> wrapper = super.buildQueryWrapper(param);
        wrapper.eqIfPresent("package_id", param.getPackageId());
        return wrapper;
    }

    @Override
    public List<GrouponOrderVO> convertToViewObject(@Nonnull List<GrouponOrder> entities) {
        List<GrouponOrderVO> list = convertList(entities, this::convertToViewObject);
        Set<Long> userIds = convertSet(entities, GrouponOrder::getUserId);
        Map<Long, UserSimpleVO> users = getUserSimpleMap(userIds);
        Set<Long> packageIds = convertSet(entities, GrouponOrder::getPackageId);
        Map<Long, GrouponPackageRespDTO> packages = grouponPackageApi.getMap(packageIds);
        for (GrouponOrderVO vo : list) {
            vo.setUser(users.getOrDefault(vo.getUserId(), UserSimpleVO.ANONYMOUS));
            vo.setPackageInfo(packages.getOrDefault(vo.getUserId(), new GrouponPackageRespDTO()));
        }
        return list;
    }

    @Nonnull
    @Override
    protected Map<Long, UserSimpleVO> getUserSimpleMap(Collection<Long> userIds) {
        return memberUserApi.getUserSimpleMap(userIds);
    }

}
